﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Core.Converters
{
    /// <summary>
    /// <FontFamily Value="微软雅黑"/>
    /// </summary>
    public class FontFamilyConverter : ValueConverter
    {
        public object Convert(ConvertContext context, RT.Property property)
        {
            if(!string.IsNullOrEmpty(property.ValueString))
            {
                return new FontFamily(property.ValueString);
            }
            else
            {
                string fontName = property.ValueNode.Attributes["Value"].ToUpper();
                return new FontFamily(fontName);
            }
        }

        public RT.ValueNode ConvertBack(ConvertContext context, object value, object dtTagData)
        {
            FontFamily font = value as FontFamily;
            RT.ValueNode valueNode = new RT.ValueNode("FontFamily");
            valueNode.Attributes = new Dictionary<string, string>();
            valueNode.Attributes.Add("Value", font == null ? string.Empty : font.Source);
            return valueNode;
        }
    }
}
